package com.settlement.system.controller;

import com.settlement.system.model.entity.common.TblRrAccountantSubject;
import com.settlement.system.model.entity.common.TblRrAccountantSubject;
import com.settlement.system.service.IBaseService;
import com.alibaba.excel.context.AnalysisContext;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import com.settlement.system.model.entity.common.TblRrAccountantSubject;
import jakarta.annotation.Resource;
import com.settlement.system.service.ITblRrAccountantSubjectService;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 收退款-会计科目 前端控制器
 * @author Ximi
 * @since 2020-07-06
 */

@Api(tags = "收退款-会计科目",description = "收退款-会计科目")
@RestController
@RequestMapping("/rr/accountant_subject")
public class TblRrAccountantSubjectController extends ExcelController<TblRrAccountantSubject> {

    @Resource
    private ITblRrAccountantSubjectService tblRrAccountantSubjectService;

    @Override
    public IBaseService init() {
        return tblRrAccountantSubjectService;
    }

    @Override
    public Class<?> getModelClass() {
        return TblRrAccountantSubject.class;
    }

    @Override
    public String verifyImportObject(TblRrAccountantSubject tblRrAccountantSubject, AnalysisContext analysisContext) {
        String ncMarketTool = tblRrAccountantSubject.getNcMarketTool();
        if(StringUtils.isNotEmpty(ncMarketTool)
                && !ncMarketTool.equals("Y")
                && !ncMarketTool.equals("N")
        ){
            return "[辅助项_营销工具]的值应为Y、N或者空";
        }
        return super.verifyImportObject(tblRrAccountantSubject, analysisContext);
    }

    @Override
    protected String validateAllDate(List<TblRrAccountantSubject> datas) {
        List<String> list = new ArrayList<>();
        for (TblRrAccountantSubject data : datas) {
            String toString = data.toString();
            if (list.contains(toString)) {
                return "有两行数据重复";
            } else {
                list.add(toString);
            }
        }

        datas.stream()
                .collect(Collectors.groupingBy(TblRrAccountantSubject::getSubjectNo))
                .entrySet().stream().map(s->s.getValue().size()).collect(Collectors.toList())
        .stream().anyMatch(s->s>1);

        Set<Map.Entry<String, List<TblRrAccountantSubject>>> entries = datas.stream()
                .collect(Collectors.groupingBy(TblRrAccountantSubject::getSubjectNo))
                .entrySet();

        for (Map.Entry<String, List<TblRrAccountantSubject>> entry : entries) {
            int size = entry.getValue().size();
            if (size > 1) {
                return "有两行数据的[科目编号]重复";
            }
        }

        return super.validateAllDate(datas);
    }
}

